package com.bw.util;

import com.bw.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession(false);
        if(session!=null){
            User user = (User) session.getAttribute("user");
            if(user!=null){
                return true;
            }
        }
        request.setAttribute("msg","没有权限,请先登录");
        request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request,response);
        return false;
    }
}
